package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MyMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteRole")
    public Result deleteRole(Integer id) {
        try {
            roleService.deleteRole(id);
            return new Result(true, MyMessageConstant.ROLE_DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            if (e.getMessage().contains("foreign key constraint")) {
                List<User> userList = roleService.findUserByRoleId(id);
                StringBuffer sb = new StringBuffer();
                if (userList!=null){
                    for (User user : userList) {
                        sb.append(user.getUsername()).append(",");
                    }
                    return new Result(false, "该角色已被用户"+sb.toString().substring(0,(sb.length()-1))+"绑定,请先解除绑定,再进行删除操作");
                }
            }
            //权限,菜单表关联
            if ((e.getMessage().contains("t_role_permission")) || (e.getMessage().contains("t_role_menu"))){
                return new Result(false, "该角色已被权限或者菜单绑定,请先解除绑定再删除");
            }
            return new Result(false, MyMessageConstant.ROLE_DELETE_FAIL);
        }
    }

    /**
     * 编辑角色
     *
     * @param role
     * @param permissionIds
     * @param menuIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.edit(role, permissionIds, menuIds);
            return new Result(true, MyMessageConstant.ROLE_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MyMessageConstant.ROLE_EDIT_FAIL);
        }
    }

    /**
     * 查询角色对应的菜单
     *
     * @param id
     * @return
     */
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        try {
            List<Integer> list = roleService.findMenuIdsByRoleId(id);
            return new Result(true, MyMessageConstant.MENU_QUERY_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MyMessageConstant.MENU_QUERY_FAIL);
        }
    }

    /**
     * 查询角色
     *
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> list = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MyMessageConstant.PERMISSION_QUERY_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MyMessageConstant.PERMISSION_QUERY_FAIL);
        }
    }

    /**
     * 查询角色
     *
     * @param id
     * @return
     */
    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer id) {
        try {
            Role role = roleService.findRoleById(id);
            return new Result(true, MyMessageConstant.ROLE_QUERY_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MyMessageConstant.ROLE_QUERY_FAIL);
        }

    }

    /**
     * 添加角色
     *
     * @param role
     * @param permissionIds
     * @param menuIds
     * @return
     */
    @RequestMapping("/addRole")
    public Result addRole(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.addRole(role, permissionIds, menuIds);
            return new Result(true, MyMessageConstant.ROLE_ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MyMessageConstant.ROLE_ADD_FAIL);
        }
    }

    @RequestMapping("/getAllRole")
    public Result getAllRole() {
        try {
            List<Role> list = roleService.getAllRole();
            return new Result(true, MyMessageConstant.ROLE_QUERY_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MyMessageConstant.ROLE_QUERY_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }
}
